package com.turman.builibrary.tab.bottom;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ScrollView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.turman.blibrary.util.BDisplayUtil;
import com.turman.blibrary.util.BViewUtil;
import com.turman.builibrary.R;
import com.turman.builibrary.tab.common.IBTabLayout;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BTabBottomLayout extends FrameLayout implements IBTabLayout<BTabBottomItem, BTabBottomInfo<?>> {
    private static final String TAG_TAB_BOTTOM = "TAG_TAB_BOTTOM";
    private List<OnTabSelectedListener<BTabBottomInfo<?>>> tabSelectedListeners = new ArrayList<>();
    private BTabBottomInfo<?> selectedInfo;
    private float bottomAlpha = 1f;
    private static float tabBottomHeight = 60;
    //头部线条高度
    private float bottomLineHeight = 0.5f;
    //bottomlayout头部线头颜色，分割线
    private String bottomLineColor = "#dfe0e1";
    private List<BTabBottomInfo<?>> infoList;

    public BTabBottomLayout(@NonNull Context context) {
        this(context, null);
    }

    public BTabBottomLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BTabBottomLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public BTabBottomItem findTab(@NonNull BTabBottomInfo<?> data) {
        ViewGroup ll = findViewWithTag(TAG_TAB_BOTTOM);
        for (int i = 0; i < ll.getChildCount(); i++) {
            View child = ll.getChildAt(i);
            if (child instanceof BTabBottomItem) {
                BTabBottomItem tab = (BTabBottomItem) child;
                if (tab.getInfo() == data) {
                    return tab;
                }
            }
        }
        return null;
    }

    @Override
    public void addTabSelected(@NonNull OnTabSelectedListener<BTabBottomInfo<?>> defaultInfo) {
        tabSelectedListeners.add(defaultInfo);
    }

    @Override
    public void defaultSelected(@NonNull BTabBottomInfo<?> defaultInfo) {
        onSelected(defaultInfo);
    }

    public void setBottomAlpha(float bottomAlpha) {
        this.bottomAlpha = bottomAlpha;
    }

    public void setTabBottomHeight(float tabBottomHeight) {
        this.tabBottomHeight = tabBottomHeight;
    }

    public void setBottomLineHeight(float bottomLineHeight) {
        this.bottomLineHeight = bottomLineHeight;
    }

    public void setBottomLineColor(String bottomLineColor) {
        this.bottomLineColor = bottomLineColor;
    }

    @Override
    public void inflateInfo(@NonNull List<BTabBottomInfo<?>> infoList) {
        if (infoList == null || infoList.size() == 0) {
            return;
        }
        this.infoList = infoList;
        //移除之前的view，防止重复添加
        for (int i = getChildCount() - 1; i > 0; i++) {
            removeViewAt(i);
        }
        selectedInfo = null;
        addBackground();

        if (tabSelectedListeners.size() > 0) {
            Iterator<OnTabSelectedListener<BTabBottomInfo<?>>> iterator = tabSelectedListeners.iterator();
            while (iterator.next() instanceof BTabBottomItem) {
                iterator.remove();
            }
        }

        //添加tabs
        FrameLayout ll = new FrameLayout(getContext());
        int width = BDisplayUtil.getDisplayWidthInPx(getContext()) / infoList.size();
        int height = BDisplayUtil.dp2px(tabBottomHeight, getResources());
        ll.setTag(TAG_TAB_BOTTOM);
        for (int i = 0; i < infoList.size(); i++) {
            BTabBottomInfo<?> info = infoList.get(i);
            LayoutParams layoutParams = new LayoutParams(width, height);
            layoutParams.gravity = Gravity.BOTTOM;
            layoutParams.leftMargin = i * width;

            BTabBottomItem item = new BTabBottomItem(getContext());
            tabSelectedListeners.add(item);
            item.setBTabInfo(info);
            ll.addView(item, layoutParams);
            item.setOnClickListener(v -> {
                onSelected(info);
            });
        }

        LayoutParams flParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        flParams.gravity = Gravity.BOTTOM;

        addBottomLine();
        addView(ll, flParams);

        fixContentView();
    }

    private void addBottomLine() {
        View bottomLine = new View(getContext());
        bottomLine.setBackgroundColor(Color.parseColor(bottomLineColor));

        LayoutParams bottomLineParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, BDisplayUtil.dp2px(bottomLineHeight, getResources()));
        bottomLineParams.gravity = Gravity.BOTTOM;
        bottomLineParams.bottomMargin = BDisplayUtil.dp2px(tabBottomHeight - bottomLineHeight, getResources());
        addView(bottomLine, bottomLineParams);
        bottomLine.setAlpha(bottomAlpha);
    }

    private void onSelected(@NonNull BTabBottomInfo<?> nextInfo) {
        for (OnTabSelectedListener<BTabBottomInfo<?>> listener : tabSelectedListeners) {
            listener.onTabSelectedChange(infoList.indexOf(nextInfo), selectedInfo, nextInfo);
        }
        this.selectedInfo = nextInfo;
    }


    private void addBackground() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.b_bottom_layout_bg, null);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, BDisplayUtil.dp2px(tabBottomHeight, getResources()));
        params.gravity = Gravity.BOTTOM;
        addView(view, params);
        view.setAlpha(bottomAlpha);
    }

    /**
    * 处理bottomtab底部遮挡问题
    * @author buobao
    * @since 2024/3/11 14:55
    */
    private void fixContentView() {
        if (!(getChildAt(0) instanceof ViewGroup)) {
            return;
        }
        ViewGroup rootView = (ViewGroup) getChildAt(0);
        ViewGroup targetView = BViewUtil.findTypeView(rootView, RecyclerView.class);
        if (targetView == null) {
            targetView = BViewUtil.findTypeView(rootView, ScrollView.class);
        }
        if (targetView == null) {
            targetView = BViewUtil.findTypeView(rootView, AbsListView.class);
        }
        if (targetView != null) {
            targetView.setPadding(0, 0, 0, BDisplayUtil.dp2px(tabBottomHeight, getResources()));
            targetView.setClipToPadding(false);
        }
    }

//    public static void clipBottomPadding(ViewGroup targetView) {
//        if (targetView != null) {
//            targetView.setPadding(0, 0, 0, BDisplayUtil.dp2px(tabBottomHeight));
//            targetView.setClipToPadding(false);
//        }
//    }
//
//    public void resizeHiTabBottomLayout() {
//        int width = HiDisplayUtil.getDisplayWidthInPx(getContext()) / infoList.size();
//        ViewGroup frameLayout = (ViewGroup) getChildAt(getChildCount() - 1);
//        int childCount = frameLayout.getChildCount();
//        for (int i = 0; i < childCount; i++) {
//            View button = frameLayout.getChildAt(i);
//            FrameLayout.LayoutParams params = (LayoutParams) button.getLayoutParams();
//            params.width = width;
//            params.leftMargin = i * width;
//            button.setLayoutParams(params);
//        }
//    }

}
